package com.pojo.order.controller;

import com.pojo.order.entity.Payment;
import com.pojo.order.entity.Reservation;
import com.pojo.order.entity.Ticket;
import com.pojo.order.entity.User;
import com.pojo.order.service.PaymentService;
import com.pojo.order.service.ReservationService;
import com.pojo.order.service.TicketService;
import com.pojo.order.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private ReservationService reservationService;
    @Autowired
    private TicketService ticketService;
    @Autowired
    private PaymentService paymentService;

    @GetMapping(value = "order/login/{userId}/{pwd}")
    public String login(@PathVariable("userId") Long userId, @PathVariable("pwd") Long pwd, HttpSession session) {
        User user = this.userService.queryUserById(userId);
        if (user == null) {
            return "login failed.";
        }

        if (pwd == user.getPassword()) {
            return "login success.";
        }

        return "pwd wrong.";
    }

    @GetMapping(value = "order/reservations/restTicket")
    public Map<Long, Ticket> getRestTicket() {
        return this.ticketService.getRestTicket();
    }

    @GetMapping(value = "order/reservations/{userId}")
    public Map<Long, Reservation> getReservations(@PathVariable("userId") Long userId) {
        User user = this.userService.queryUserById(userId);
        if (user == null) {
            return null;
        }
        return this.reservationService.queryReservationByUserId(userId);
    }


    @GetMapping(value = "order/reservations/reserveTicket/{userId}/{t_id}")
    public String reserveTicket(@PathVariable("userId") Long userId, @PathVariable("t_id") Long t_id) {
        User user = this.userService.queryUserById(userId);
        if (user == null) {
            return "用户不存在！";
        }
        return this.reservationService.reserveTicket(userId, t_id);
    }

    @GetMapping(value = "order/reservations/cancelReservation/{userId}/{t_id}")
    public String cancelReservation(@PathVariable("userId") Long userId, @PathVariable("t_id") Long t_id) {
        User user = this.userService.queryUserById(userId);
        if (user == null) {
            return "用户不存在！";
        }
        return this.reservationService.cancelReservation(userId, t_id);
    }

    @GetMapping(value = "order/payments/{userId}")
    public Map<Long, Payment> getPayments(@PathVariable("userId") Long userId) {
        User user = this.userService.queryUserById(userId);
        if (user == null) {
            return null;
        }
        return this.paymentService.queryPaymentByUserId(userId);
    }


    @GetMapping(value = "order/payments/payTicket/{userId}/{t_id}")
    public String payTicket(@PathVariable("userId") Long userId, @PathVariable("t_id") Long t_id) {
        User user = this.userService.queryUserById(userId);
        if (user == null) {
            return "用户不存在！";
        }
        return this.paymentService.payTicket(userId, t_id);
    }

    @GetMapping(value = "order/payments/cancelPayment/{userId}/{t_id}")
    public String cancelPayment(@PathVariable("userId") Long userId, @PathVariable("t_id") Long t_id) {
        User user = this.userService.queryUserById(userId);
        if (user == null) {
            return "用户不存在！";
        }
        return this.paymentService.cancelPayment(userId, t_id);
    }

}
